package com.chen.controller;

import com.alibaba.fastjson2.JSON;
import com.chen.annotation.Log;
import com.chen.annotation.RateLimiter;
import com.chen.core.controller.BaseController;
import com.chen.core.domain.AjaxResult;
import com.chen.core.domain.entity.SysUser;
import com.chen.enums.BusinessType;
import com.chen.enums.LimitType;
import com.chen.service.valid.ValidAdd;
import com.chen.service.valid.ValidAll;
import com.chen.service.valid.ValidEdit;
import com.chen.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

@RestController
@Slf4j
@Validated
public class TestController extends BaseController {

    @Autowired
    protected HttpServletRequest httpServletRequest;


    @RateLimiter(count = 3, limitType = LimitType.IP)
    @GetMapping("/test")
    public void test(HttpServletRequest request, HttpServletResponse response) {
        log.info("request: {}", request);
        String name = httpServletRequest.getHeader("name");
        boolean equals = httpServletRequest.equals(request);
        System.out.println(equals);
        log.info("response: {}", response);
        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.success("测试成功")));
    }

    @PostMapping("/testValid")
    public void add(@Validated(value = {ValidAdd.class, ValidAll.class}) @RequestBody SysUser sysUser) {
        log.info("sysUser: {}", sysUser);
    }

    @Log(title = "校验更新", businessType = BusinessType.UPDATE)
    @PostMapping("/updateValid")
    public void edit(@Validated(value = {ValidEdit.class, ValidAll.class}) @RequestBody SysUser sysUser) {
        log.info("sysUser: {}", sysUser);
    }

    @PostMapping("/updateValidWithOutRequestBody")
    public void updateValidWithOutRequestBody(@Validated(value = {ValidEdit.class}) SysUser sysUser) {
        log.info("sysUser: {}", sysUser);
    }

    @PostMapping("/map")
    public void updateValidWithOutRequestBody(@RequestBody DevicePointInformation devicePointInformation) {
        log.info("map: {}", devicePointInformation);
    }

    @GetMapping("/testResponseEntity5")
    public ResponseEntity<String> testResponseEntity5(String param) {

        // 如果参数不存在就返回默认的图片url，并返回状态码201
        if (!ObjectUtils.anyNotNull(param)) {
            return new ResponseEntity<>("默认的图片URL", HttpStatus.CREATED);
        }

        // 向数据库中插入图片，并返回能访问图片地址的url
        // ......
        // ⏹用于插入数据成功之后返回数据给前台

        // ⏹201 状态码,并返回图片的url
        return ResponseEntity.status(HttpStatus.CREATED).body("图片的url");
    }

    @GetMapping("/testParamValid")
    public ResponseEntity<String> testR(@NotBlank(message = "参数不可为空") String param) {

        // 如果参数不存在就返回默认的图片url，并返回状态码201
        if (!ObjectUtils.anyNotNull(param)) {
            return new ResponseEntity<>("默认的图片URL", HttpStatus.CREATED);
        }

        // 向数据库中插入图片，并返回能访问图片地址的url
        // ......
        // ⏹用于插入数据成功之后返回数据给前台

        // ⏹201 状态码,并返回图片的url
        return ResponseEntity.status(HttpStatus.CREATED).body("图片的url");
    }
}
